package cn.jasonone.jame.model;

import cn.jasonone.jame.FontUtil;
import cn.jasonone.jame.GameScene;
import cn.jasonone.jame.basic.ActionListener;
import cn.jasonone.jame.basic.GameContext;
import javafx.geometry.VPos;
import javafx.scene.Cursor;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * @author Jason
 * @date 2020-06-13 22:18
 * @since 1.0.0
 */
public class GButton extends Spirit {
    private Image activeImage;
    private Image backgroundImage;

    private boolean active;
    private Color activeColor;
    private Color backgroundColor;
    private String text;

    private List<ActionListener> actionListeners=new CopyOnWriteArrayList<>();
    /**
     * 文字颜色,默认:黑色
     */
    @NonNull
    @Setter
    @Getter
    private Color textColor=Color.BLACK;

    public GButton(Image activeImage, Image backgroundImage, String text) {
        this(activeImage, backgroundImage, null, null, text);
    }

    public GButton(Color activeColor, Color backgroundColor, String text) {
        this(null, null, activeColor, backgroundColor, text);
    }

    public GButton(String text) {
        this(Color.valueOf("#f1f1f1"),Color.WHITE,  text);
    }

    public GButton(Image activeImage, Image backgroundImage, Color activeColor, Color backgroundColor, String text) {
        this.activeImage = activeImage;
        this.backgroundImage = backgroundImage;
        this.activeColor = activeColor;
        this.backgroundColor = backgroundColor;
        this.text = text;
        this.setText(text);
    }

    @Override
    public void draw(GraphicsContext gc) {
        super.draw(gc);
        if (this.active) {
            if (this.activeColor != null) {
                gc.setFill(this.activeColor);
                gc.fillRoundRect(getX(), getY(), getWidth(), getHeight(),8,8);
            }
            if(this.activeImage != null){
                gc.drawImage(this.activeImage,getX(),getY(),getWidth(),getHeight());
            }
        }else{
            if (this.backgroundColor != null) {
                gc.setFill(this.backgroundColor);
                gc.fillRoundRect(getX(), getY(), getWidth(), getHeight(),8,8);
            }
            if(this.backgroundImage != null){
                gc.drawImage(this.backgroundImage,getX(),getY(),getWidth(),getHeight());
        }
        }
        Dimension dimension = FontUtil.getDimension(getFont(), this.text);
        gc.setFill(textColor);
        gc.setTextBaseline(VPos.CENTER);
        //居中显示文字
        gc.fillText(this.text,(getWidth()-dimension.getWidth())/2+getX(),getY()+getHeight()/2);
    }

    @Override
    public void setFont(Font font) {
        super.setFont(font);
        Dimension dimension = FontUtil.getDimension(font, this.text);
        this.setSize(dimension.getWidth()+10,dimension.getHeight()+10);
    }

    public void setText(String text){
        this.text=text;
        Dimension dimension = FontUtil.getDimension(getFont(), this.text);
        this.setSize(dimension.getWidth()+10,dimension.getHeight()+10);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        this.active = true;
        if(getScene() != null){
            getScene().setCursor(Cursor.HAND);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        this.active = false;
        if(getScene() != null){
            getScene().setCursor(Cursor.DEFAULT);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Iterator<ActionListener> iterator = actionListeners.iterator();
        while (iterator.hasNext()){
            ActionListener listener = iterator.next();
            listener.action(e);
        }
    }

    @Override
    public void destory(GameContext<GameScene> gc) {
        actionListeners.clear();
    }

    /**
     * 监听按钮点击事件
     * @param listener 按钮点击事件
     */
    public void addActionListener(@NonNull ActionListener listener){
        this.actionListeners.add(listener);
    }
}
